﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RockCompany.Common.Lib;
using RockCompany.Common.Constant;
using RockCompany.Master.Model;
using RockCompany.Master.Controller;
using RockCompany.Master.DTO;
using RockCompany.Master.Constant;
using RockCompany.Master.Validator;
using RockCompany.Master.View;
using RockCompany.Common.Forms;

namespace RockCompany.Master.View
{
    public partial class SI03A : ParentFrm
    {
        #region Properties
        private Translator trans;
        private SI03AFormModel frmModel;
        private DataGridViewRow selectRow = null;
        private DataTable matCheckListDetails;
        private bool isPassed = false;
        private bool isNew = true;
        private SI01 matsManage;
        private string sUserName;
        #endregion

        #region Constructor
        public SI03A(Translator translator, string sUName, Form caller)
            : base(sUName, caller)
        {
            InitializeComponent();
            // Initial forms' components
            Owner = caller;
            sUserName = sUName;
            this.trans = translator;
            translator.controlTranslate(this);
        }
        #endregion

        #region Utility Methods
        public void initExistData(DataGridViewRow selectRow, DataTable matCheckListDetails)
        {
            this.selectRow = selectRow;
            this.matCheckListDetails = matCheckListDetails;
            isNew = false;
        }

        public void initDefaultData()
        {
            frmModel = new SI03AFormModel();
            frmModel.Owner = this;
            frmModel.translator = this.trans;

            SI03AFormModel.bindCombo(comboBox_NewSupplier, PHIEU_KN_HN_DTO.N_NHA_CUNG_UNG, PHIEU_KN_HN_DTO.N_NHA_CUNG_UNG, frmModel.supplierLOV());
            SI03AFormModel.bindCombo(comboBox_NewOrder, PHIEU_KN_HN_DTO.N_MS_DON_HANG, PHIEU_KN_HN_DTO.N_MS_DON_HANG, frmModel.newOrderLOV());

            PHIEU_KN_HN_CT_DTO temp = new PHIEU_KN_HN_CT_DTO();
            NGUYEN_VLIEU_VTU_DTO nvl = new NGUYEN_VLIEU_VTU_DTO();
            List<System.Reflection.PropertyInfo> properties = new List<System.Reflection.PropertyInfo>();
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_CT_DTO.N_CHON));
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_CT_DTO.N_MA_NVL_VTU));
            properties.Add(nvl.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_TEN_NVL_VTU));
            properties.Add(nvl.GetType().GetProperty(NGUYEN_VLIEU_VTU_DTO.N_DON_VI));
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_CT_DTO.N_SL_DAT_MUA));
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_CT_DTO.N_SL_GIAO_HANG));
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_CT_DTO.N_SL_THUC_NHAN));
            properties.Add(temp.GetType().GetProperty(PHIEU_KN_HN_CT_DTO.N_GHI_CHU));
            List<string> readOnlyCols = new List<string>();
            readOnlyCols.Add(PHIEU_KN_HN_CT_DTO.N_MA_NVL_VTU);
            frmModel.buildDataGrid(dataGridView_NewMatCheckDetail, properties, readOnlyCols);
            if (!isNew)
            {
                textBox_NewMatCheckNo.Text = selectRow.Cells[PHIEU_KN_HN_DTO.N_MS_PHIEU_KN].EditedFormattedValue.ToString();
                textBox_NewMatCheckNo.Enabled = false;
                comboBox_NewOrder.Text = selectRow.Cells[PHIEU_KN_HN_DTO.N_MS_DON_HANG].EditedFormattedValue.ToString();
                comboBox_NewSupplier.Text = selectRow.Cells[PHIEU_KN_HN_DTO.N_NHA_CUNG_UNG].EditedFormattedValue.ToString();
                textBox_NewReceipt.Text = selectRow.Cells[PHIEU_KN_HN_DTO.N_MS_HOA_DON].EditedFormattedValue.ToString();
                radioButton_Pass.Checked = (bool)selectRow.Cells[PHIEU_KN_HN_DTO.N_PHE_DUYET].EditedFormattedValue;
                radioButton_Unpass.Checked = !(bool)selectRow.Cells[PHIEU_KN_HN_DTO.N_PHE_DUYET].EditedFormattedValue;
                isPassed = (bool)selectRow.Cells[PHIEU_KN_HN_DTO.N_PHE_DUYET].EditedFormattedValue;

                if (CommonModel.existData(matCheckListDetails))
                    frmModel.NewMatCheckDetails = matCheckListDetails;
                else
                    frmModel.NewMatCheckDetails = frmModel.buildEmptyDataTable(properties);
            }
            else
            {
                frmModel.NewMatCheckDetails = frmModel.buildEmptyDataTable(properties);
            }
            dataGridView_NewMatCheckDetail.DataSource = frmModel.NewMatCheckDetails;
        }
        #endregion

        #region Event Methods
        private void button_NewMatCheckDetail_Click(object sender, EventArgs e)
        {
            SI04 invenroty = new SI04(trans, sUserName, this);
            invenroty.callChooser(SI04Const.NO_MAT_TYPE);
            if (invenroty.ShowDialog(this) == DialogResult.OK)
            {
                DataTable selectData = invenroty.getSelectedMatsNo();
                invenroty.Dispose();
                if (selectData.Rows.Count > 0 && frmModel.addNewMatCheckDetail(textBox_NewMatCheckNo.Text, selectData))
                    InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
            //matsManage = new SI01(trans, sUserName, this);
            //matsManage.callChooser(true);
            //if (matsManage.ShowDialog(this) == DialogResult.OK)
            //{
            //    DataTable selectedMats = matsManage.getSelectedMatsNo();
            //    matsManage.Dispose();
            //    if (frmModel.addNewMatCheckDetail(textBox_NewMatCheckNo.Text, selectedMats))
            //        InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
            //    else
            //        InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            //}
        }

        private void button_DelMatCheckDetail_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_DELETE)))
            {
                if (frmModel.removeMatCheckDetail(textBox_NewMatCheckNo.Text))
                {
                    InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                }
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.RemoveError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
        }

        private void dataGridView_NewMatCheckDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView_NewMatCheckDetail.CurrentCell != null && dataGridView_NewMatCheckDetail.CurrentCell.ColumnIndex == 0 && e.RowIndex > -1)
                frmModel.addRemoveCheckedList(0, frmModel.getCheckedRow(e.RowIndex));
        }

        private void button_SaveMatCheck_Click(object sender, EventArgs e)
        {
            if (InforDialog.showConfirmBox(trans.getMessageCaption(CommonConstantIF.CONFIRM_CAPTION), trans.getMessageCaption(CommonConstantIF.CONFIRM_ADD)))
            {
                if (SI03FormControler.isValidNew(textBox_NewMatCheckNo.Text, textBox_NewReceipt.Text, comboBox_NewOrder.Text))
                {
                    PHIEU_KN_HN_DTO updateMatCheck = new PHIEU_KN_HN_DTO();
                    updateMatCheck.MS_PHIEU_KN = textBox_NewMatCheckNo.Text;
                    updateMatCheck.MS_DON_HANG = comboBox_NewOrder.Text;
                    updateMatCheck.MS_HOA_DON = textBox_NewReceipt.Text;
                    updateMatCheck.NHA_CUNG_UNG = comboBox_NewSupplier.Text;
                    updateMatCheck.NGAY_GIAO_HANG = dateTimePicker_NgayGiaoHang.Value;
                    updateMatCheck.PHE_DUYET = isPassed;

                    bool b1 = false, b2 = false;

                    if (!isNew)
                    {
                        b1 = frmModel.updateMatCheck(updateMatCheck);
                        b2 = frmModel.saveData(textBox_NewMatCheckNo.Text, sUserName);
                        if (b1 && b2)
                        {
                            InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.UpdateComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                        }
                    }
                    else
                    {
                        b1 = (dataGridView_NewMatCheckDetail.Rows.Count > 0 && frmModel.insertNewMatCheck(updateMatCheck));
                        if (b1)
                            b2 = frmModel.saveData(textBox_NewMatCheckNo.Text, sUserName);
                        if (frmModel.NewMatCheckDetails.Rows.Count > 0)
                        {
                            if (b1 && b2)
                            {
                                InforDialog.showInforBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewComplete), trans.getMessageCaption(CommonConstantIF.InforBoxCap));
                                isNew = false;
                                textBox_NewMatCheckNo.Enabled = false;
                            }
                            else
                                InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.InsertNewError), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                        }
                    }
                }
                else
                    InforDialog.showErrorBox(Owner, trans.getMessageCaption(CommonConstantIF.REQUIRED_FIELD), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
            }
        }

        private void radioButton_Pass_CheckedChanged(object sender, EventArgs e)
        {
            isPassed = true;
        }

        private void radioButton_Unpass_CheckedChanged(object sender, EventArgs e)
        {
            isPassed = false;
        }

        private void dataGridView_NewMatCheckDetail_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            int num1_index = dataGridView_NewMatCheckDetail.CurrentRow.Cells[PHIEU_KN_HN_CT_DTO.N_SL_DAT_MUA].ColumnIndex;
            int num2_index = dataGridView_NewMatCheckDetail.CurrentRow.Cells[PHIEU_KN_HN_CT_DTO.N_SL_GIAO_HANG].ColumnIndex;
            int num3_index = dataGridView_NewMatCheckDetail.CurrentRow.Cells[PHIEU_KN_HN_CT_DTO.N_SL_THUC_NHAN].ColumnIndex;
            if (e.ColumnIndex == num1_index || e.ColumnIndex == num2_index || e.ColumnIndex == num3_index)
            {
                string s = UserValidator.isValidNumer(e.FormattedValue);
                e.Cancel = !string.IsNullOrEmpty(s);
                if (e.Cancel)
                {
                    InforDialog.showWarningrBox(Owner, trans.getMessageCaption(s), trans.getMessageCaption(CommonConstantIF.ErrorBoxCap));
                }
            }
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
    }
}
